﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entities;
using DataAccess;
using BusinessLogic;
using System.Data;

namespace ATM_System_Mock1_G2
{
    public partial class WithdrawScreen01 : System.Web.UI.Page
    {
        MoneyEntites moneyE = new MoneyEntites();
        MoneyDA moneyDA = new MoneyDA();
        MoneyBL moneyBL = new MoneyBL();
        AccountEntites accountE = new AccountEntites();
        AccountDA accountDA = new AccountDA();
        AccountBL accountBL = new AccountBL();
        LogEntites logE = new LogEntites();
        LogDA logDA = new LogDA();
        LogBL logBL = new LogBL();
        StockBL stock_BL = new StockBL();
        StockEntites stock_E = new StockEntites();
        CardEntites cardE = new CardEntites();
        protected void Page_Load(object sender, EventArgs e)
        {
            lb_thongbaoSC1.Text = "Choose amount below or enter";
        }
        public void WrirteLog(decimal amount)
        {
            logE.LogTypeID = 2;
            logE.ATMID = 2;
            logE.CardNo = Session["CardNo"].ToString();
            logE.LogDate = DateTime.Now;
            logE.Amount = amount;
            logE.Details = "Withdraw";
            logBL.writeLog(logE);
        }
        public void UpdateQuantity(int a, int b)
        {
            stock_E.Quantity = a;
            stock_E.MoneyID = b;
            stock_BL.UpdateQuantityATM(stock_E);
        }

        public void RutTien(decimal Amount)
        {
           
               // accountE.Balance = int.Parse(txt_Nhap.Text);
                decimal TienRut = Amount;

                accountE.Balance = Amount;
                cardE.CardNo = HttpContext.Current.Session["CardNo"].ToString();
                Decimal balance = accountBL.getBalance(cardE);
                Decimal moneyvalue = moneyBL.getMoneyValue();
                if ((balance > TienRut) && (TienRut % 50000 == 0) && (balance - TienRut > 50000) && (TienRut <20000000))
                {
                    DataTable dt = new DataTable();
                    dt = stock_BL.GetQuantity(stock_E);
                    int T500k = int.Parse(dt.Rows[0]["Quantity"].ToString());
                    int T200k = int.Parse(dt.Rows[1]["Quantity"].ToString());
                    int T100k = int.Parse(dt.Rows[2]["Quantity"].ToString());
                    int T50k = int.Parse(dt.Rows[3]["Quantity"].ToString());
                    int T20k = int.Parse(dt.Rows[4]["Quantity"].ToString());
                    int T10k = int.Parse(dt.Rows[5]["Quantity"].ToString());

                    decimal TongTien = T500k * 500000 + T200k * 200000 + T100k * 100000 + T50k * 50000 + T20k * 20000 + T10k * 10000;
                    if (TienRut > TongTien)
                    {
                        lb_tb.Text = "withdraw is not success";
                    }
                    else if (TienRut == TongTien)
                    {
                        stock_E.Quantity = T500k;
                        stock_E.MoneyID = 1;
                        stock_BL.UpdateQuantityATM(stock_E);

                        stock_E.Quantity = T200k;
                        stock_E.MoneyID = 2;
                        stock_BL.UpdateQuantityATM(stock_E);

                        stock_E.Quantity = T100k;
                        stock_E.MoneyID = 3;
                        stock_BL.UpdateQuantityATM(stock_E);

                        stock_E.Quantity = T50k;
                        stock_E.MoneyID = 4;
                        stock_BL.UpdateQuantityATM(stock_E);

                        stock_E.Quantity = T20k;
                        stock_E.MoneyID = 6;
                        stock_BL.UpdateQuantityATM(stock_E);

                        stock_E.Quantity = T10k;
                        stock_E.MoneyID = 7;
                        stock_BL.UpdateQuantityATM(stock_E);

                        cardE.CardNo = HttpContext.Current.Session["CardNo"].ToString();
                        accountBL.CheckWithdraw(accountE, cardE);
                        WrirteLog(Amount);
                        Response.Redirect("WithdrawScreen03.aspx");
                    }

                    else
                    {
                        decimal Con;


                        int To500k = (int)TienRut / 500000;
                        if (To500k > T500k)
                        {
                            To500k = T500k;

                        }
                        Con = TienRut - To500k * 500000;
                        //------
                        int To200k = (int)Con / 200000;
                        if (To200k > T200k)
                        {
                            To200k = T200k;
                        }

                        Con = Con - To200k * 200000;
                        //------
                        int To100k = (int)Con / 100000;
                        if (To100k > T100k)
                        {
                            To100k = T100k;

                        }

                        Con = Con - To100k * 100000;

                        //-----
                        int To50k = (int)Con / 50000;
                        if (To50k > T50k)
                        {
                            To50k = T50k;

                        }
                        Con = Con - To50k * 50000;
                        //-----

                        int To20k = (int)Con / 20000;
                        if (To20k > T10k)
                        {
                            To20k = T20k;

                        }
                        Con = Con - To20k * 20000;
                        //-----
                        int To10k = (int)Con / 10000;
                        if (T10k < To10k && Con > 10000)
                        {
                            lb_tb.Text = "withdraw is not success ";
                        }
                        else
                        {


                            if (To10k > T10k)
                            {
                                To10k = T10k;

                            }
                            UpdateQuantity(To500k, 1);
                            UpdateQuantity(To200k, 2);
                            UpdateQuantity(To100k, 3);
                            UpdateQuantity(To50k, 4);
                            UpdateQuantity(To20k, 6);
                            UpdateQuantity(To10k, 7);
                            cardE.CardNo = HttpContext.Current.Session["CardNo"].ToString();
                            accountBL.CheckWithdraw(accountE, cardE);
                            WrirteLog(Amount);
                            Response.Redirect("WithdrawScreen03.aspx");

                        }


                    }
                }
                else
                {
                    lb_tb.Text = "* Amount is Invalid";
                }

            

        }
        protected void btn_12_Click1(object sender, ImageClickEventArgs e)
        {
            RutTien(500000);
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            RutTien(2000000);
        }

        protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
        {
            RutTien(3000000);
        }

        protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
        {
            RutTien(1000000);
        }

        protected void ImageButton4_Click(object sender, ImageClickEventArgs e)
        {
            RutTien(2500000);
        }

        protected void ImageButton5_Click(object sender, ImageClickEventArgs e)
        {
           
            Response.Redirect("WithdrawScreen02.aspx");  
        }

        protected void Button11_Click(object sender, EventArgs e)
        {

        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            Response.Redirect("Home.aspx");  
        }
    }
}